<?php
/**
*
* @package ppkBB3cker
* @version $Id: acp_board_add1_rtrack.php 1.000 2009-12-18 11:56:00 PPK $
* @copyright (c) 2009 PPK
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

$user->add_lang('mods/acp/ppkbb3cker_tracker');
$zones_title='ACP_TRACKER_RTRACKER';
if(request_var('add_rtrack', 0))
{
	$d_rtrack=$exs_rtrack=array();
	foreach(@$_POST['rtrack_url'] as $k=>$v)
	{
		if(@$_POST['rtrack_delete'][$k])
		{
			$d_rtrack[]=my_int_val($k);
		}
		else
		{
			if(@$_POST['rtrack_url'][$k]!='' && (!preg_match('#^(http|udp):\/\/(\w+|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})#', @$_POST['rtrack_url'][$k]) || strlen(@$_POST['rtrack_url'][$k]) > 512) || in_array(@$_POST['rtrack_url'][$k], $exs_rtrack))
			{
				trigger_error($user->lang['INVALID_RTRACK_URL']);
				continue;
			}

			$_POST['rtrack_remote'][$k]=!$_POST['rtrack_remote'][$k] ? 0 : ($_POST['rtrack_remote'][$k]==1 ? 1 : -1);
			if($k==0 && @$_POST['rtrack_url'][$k]!='')
			{
				$exs_rtrack[]=@$_POST['rtrack_url'][$k];
				$sql='INSERT INTO '.TRACKER_RTRACK_TABLE."
					(rtrack_url, rtrack_remote)
						VALUES(
						'".$db->sql_escape($_POST['rtrack_url'][$k])."',
						'".$db->sql_escape($_POST['rtrack_remote'][$k])."')";
				$result=$db->sql_query($sql);
			}
			else if(@$_POST['rtrack_url'][$k]!='')
			{
				$exs_rtrack[]=@$_POST['rtrack_url'][$k];
				$sql='UPDATE '.TRACKER_RTRACK_TABLE." SET
					rtrack_url='".$db->sql_escape($_POST['rtrack_url'][$k])."',
					rtrack_remote='".$db->sql_escape($_POST['rtrack_remote'][$k])."'
					 WHERE id='".my_int_val($k)."'";
				$result=$db->sql_query($sql);
			}
		}
	}
	if($d_rtrack)
	{
		$sql='DELETE FROM '.TRACKER_RTRACK_TABLE." WHERE id IN('".implode("', '", $d_rtrack)."') AND zone_id='0' AND rtrack_user='0'";
		$result=$db->sql_query($sql);
	}
	$this->u_action=append_sid("{$phpbb_admin_path}index.$phpEx", 'i=board&amp;mode=rtracker');
}

$zones_title='ACP_TRACKER_RTRACKER';

$sql='SELECT rt.*, SUM(ra.seeders) seeders, SUM(ra.leechers) leechers, SUM(ra.seeders)+SUM(ra.leechers) peers, SUM(ra.times_completed) completed, SUM(ra.err_count) errors, SUM(1) torrents FROM '.TRACKER_RTRACK_TABLE." rt LEFT JOIN ".TRACKER_RANNOUNCES_TABLE." ra ON (rt.id=ra.tracker) WHERE rt.zone_id='0' AND rt.torrent='0' AND rt.rtrack_user='0' GROUP BY rt.id ORDER BY rt.zone_id";
$result=$db->sql_query($sql);
while($row=$db->sql_fetchrow($result))
{
	$template->assign_block_vars('rtracks', array(
		'COUNT'	=> $row['id'],
		'URL'	=> htmlspecialchars($row['rtrack_url']),
		'TYPE'=>'
			<select name="rtrack_remote['.$row['id'].']">
				<option value="-1"'.($row['rtrack_remote']==-1 ? ' selected="selected"' : '').'>'.$user->lang['RTRACKER_UNITED'].'</option>
				<option value="1"'.($row['rtrack_remote']==1 ? ' selected="selected"' : '').'>'.$user->lang['RTRACKER_REMOTE_UNITED'].'</option>
			</select>
			',
		'STAT'=>$row['rtrack_remote']==1 ? sprintf($user->lang['RTRACKER_STAT'], $row['seeders'], $row['leechers'], $row['peers'], $row['completed'], $row['torrents'], $row['errors'], ($row['torrents'] ? $row['errors']/$row['torrents'] : 0)) : '',
		)
	);
}
$db->sql_freeresult($result);
$template->assign_block_vars('rtracks', array(
		'COUNT' => 0,
		'URL'	=> '',
		'TYPE'=>'
			<select name="rtrack_remote[0]">
				<option value="-1">'.$user->lang['RTRACKER_UNITED'].'</option>
				<option value="1">'.$user->lang['RTRACKER_REMOTE_UNITED'].'</option>
			</select>
		',
	)
);

$template->assign_vars(array(
	'S_SET_RTRACKER'	=> true,
	'S_HIDDEN_FIELDS'=>'<input type="hidden" name="add_rtrack" value="1" >',
	)
);


$template->assign_vars(array(
	'S_RTRACKER_INC'	=> true,
	)
);

$display_vars = array(
	'title'	=> $zones_title,
	'vars'	=> array(
		'legend1'				=> 'ACP_TRACKER_RTRACKER_SETTINGS',
		)
	);

?>
